package cn.gjculture.shopping.api.controller;

import cn.gjculture.shopping.api.entity.*;
import cn.gjculture.shopping.api.service.GoodsService;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @ApiOperation(value="获取商品集合", notes="获取商品集合")
    @GetMapping
    public Return<List<Goods>> query(Goods goods,PageInfo pageInfo){
        Return<List<Goods>> ret=new Return<>();
        List<Goods> goodss=goodsService.query(goods);
        ret.setData(goodss);
        return ret;
    }


    @ApiOperation(value="获取商品集合", notes="获取商品集合")
    @GetMapping(value = "/byType/{goodsTypeId}")
    public Return<List<Goods>> queryByType(@PathVariable(value = "goodsTypeId") String goodsTypeId,  PageInfo pageInfo){
        Return<List<Goods>> ret=new Return<>();
        if(!StringUtils.isEmpty(goodsTypeId)){
            List<Goods> goodss=goodsService.queryByTypeId(goodsTypeId);
            ret.setData(goodss);
        }
        return ret;
    }

    @ApiOperation(value="获取商品详细信息", notes="根据商品ID获取商品详细信息")
    @ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, dataType = "String", paramType = "path")
    @GetMapping(value = "/{goodsId}")
    public Return<Goods> getGoods(@PathVariable(value = "goodsId") String goodsId){
        Return<Goods> ret=new Return<>();
        Goods goods=goodsService.queryById(goodsId);
        ret.setData(goods);
        return ret;
    }


    @ApiOperation(value="新增商品", notes="新增商品")
    @ApiImplicitParam(name = "goods", value = "商品对象", required = true, dataType = "Object")
    @PostMapping
    public Return save(String param){
        ParamData<Goods> g = JSONObject.parseObject(param, new TypeReference<ParamData<Goods>>() {});
        Return ret=new Return();
        if( goodsService.insert(g.getData())>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @ApiOperation(value="修改商品", notes="修改商品")
    @ApiImplicitParam(name = "goods", value = "商品对象", required = true, dataType = "Object")
    @PutMapping
    public Return update(String param){
        ParamData<Goods> g = JSONObject.parseObject(param, new TypeReference<ParamData<Goods>>() {});
        Return ret=new Return();
        if(goodsService.update(g.getData())>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @ApiOperation(value="修改商品", notes="修改商品")
    @ApiImplicitParam(name = "goods", value = "商品对象", required = true, dataType = "Object")
    @PutMapping(value = "/other")
    public Return updateOther(String param){
        ParamData<Goods> g = JSONObject.parseObject(param, new TypeReference<ParamData<Goods>>() {});
        Return ret=new Return();
        if(goodsService.updateOther(g.getData())>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @ApiOperation(value="删除商品", notes="根据商品ID删除商品")
    @ApiImplicitParam(name = "goodsIds", value = "商品id", required = true, dataType = "String",paramType = "path")
    @DeleteMapping(value = "/{goodsIds}")
    public Return delete(@PathVariable("goodsIds") List<String> goodsIds){
        Return ret=new Return();
        if(goodsService.deleteMore(goodsIds)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }
    @ApiOperation(value="修改推荐商品", notes="根据商品ID修改推荐商品")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "goodsId", value = "商品id", required = true, dataType = "Integer"),
        @ApiImplicitParam(name = "recommended", value = "是否推荐,1:是不推荐，0:是推荐", required = true, dataType = "Integer" )
    })
    @PutMapping("/recommended")
    public Return recommended(String goodsId,Integer recommended, Return ret){
        Goods goods=new Goods();
        goods.setGoodsId(goodsId);
        goods.setRecommended(recommended);
        if(goodsService.recommended(goods)>0)
            ret.getCommon().setRet(true);
        return ret;
    }

    @PutMapping(value = "/updateStatus")
    public Return updateStatus(String goodsId, Integer status, Return ret){
        Goods goods=new Goods();
        goods.setGoodsId(goodsId);
        goods.setStatus(status);
        if(goodsService.updateStatus(goods)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }
    @ApiOperation(value = "获取推荐商品")
    @GetMapping(value = "/recommended")
    public Return<List<Goods>> queryRecommended(PageInfo pageInfo, Return<List<Goods>> ret){
        List<Goods> goods=goodsService.queryRecommended();
        ret.setData(goods);
        return ret;
    }

    @GetMapping(value = "/discountCoupon")
    public Return<List<Goods>> queryDiscountCoupon(PageInfo info){
        Return<List<Goods>> ret=new Return<>();
        List<Goods> goodsList=goodsService.queryDiscountCoupon();
        ret.setData(goodsList);
        if(!CollectionUtils.isEmpty(goodsList)) ret.getCommon().setRet(true);
        return ret;
    }


    @PostMapping(value = "/postageTemplate")
    public Return addPostageTemplate(String postageTemplateIds, String goodsId){
        Return ret=new Return();
        if(goodsService.addPostageTemplate(postageTemplateIds,goodsId)>0){
            ret.getCommon().setRet(true);
        }
        return ret;

    }

    @GetMapping(value = "/postageTemplate/{goodsId}")
    public Return queryPostageTemplateByGoodsId(@PathVariable(value = "goodsId") String goodsId){
        Return ret=new Return();
        List<PostageTemplate> postageTemplates= goodsService.queryPostageTemplateByGoodsId(goodsId);
        ret.setData(postageTemplates);
        if(!CollectionUtils.isEmpty(postageTemplates)){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @DeleteMapping(value = "/postageTemplate")
    public Return deletePostageTemplate(String goodsId,String postageTemplateId){
        Return ret=new Return();
        if(goodsService.deletePostageTemplate(goodsId,postageTemplateId)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

}
